package com.wyp168.socket.QQ.qqclient.services;

import com.wyp168.socket.QQ.qqclient.common.Message;
import com.wyp168.socket.QQ.qqclient.common.constant.MessageType;

import java.io.*;
import java.util.Date;
import java.util.Objects;

/**
 * @ClassName MessageSendClientService
 * @Title 和消息相关的方法
 * @Author wyp168
 * @Date 2022/10/30 21:58
 */
public class MessageSendClientService {

    /**
     * 私聊功能
     *
     * @param content  消息内容
     * @param getterId 消息接收者
     * @param senderId 消息发送者
     */
    public void sendMessageToOne(String content, String getterId, String senderId) {
        try {
            //构建Message
            Message message = new Message();
            message.setContent(content);
            message.setMessageType(MessageType.MESSAGE_COMMON_MES);
            message.setSender(senderId);
            message.setGetter(getterId);
            message.setSentTime(new Date().toString());

            //客户端打印
            System.out.println(senderId + " 对 " + getterId + "说: " + content);

            //获取到sender对应的socket
            ObjectOutputStream oos = new ObjectOutputStream(ManagerClientConnectServerThread.getClientConnectServerThread(senderId).getSocket().getOutputStream());
            oos.writeObject(message);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 群发消息功能
     *
     * @param content 群发消息内容
     * @param sender  发送者id
     */
    public void sendMessageToAll(String content, String sender) {
        try {
            //构建Message对象
            Message message = new Message();
            message.setMessageType(MessageType.MESSAGE_SEND_TO_ALL);
            message.setContent(content);
            message.setSender(sender);
            message.setSentTime(new Date().toString());

            ObjectOutputStream oos = new ObjectOutputStream(ManagerClientConnectServerThread.getClientConnectServerThread(sender).getSocket().getOutputStream());
            oos.writeObject(message);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void sendFileToUser(String getterId, String senderId, String filePathAndFileName, String destPath) {
//        BufferedWriter os = null;
//        BufferedReader bufReader = null;

        FileInputStream fileInputStream = null;
        ObjectOutputStream oos = null;
        try {
            //构建Message
            Message message = new Message();
            message.setMessageType(MessageType.MESSAGE_SEND_FILE);
            message.setSender(senderId);
            message.setGetter(getterId);
            message.setSourcePath(filePathAndFileName);
            message.setDescPath(destPath);
            message.setSentTime(new Date().toString());
            String fileName = filePathAndFileName.split("/")[filePathAndFileName.split("/").length - 1];
            byte[] bytes = new byte[(int) new File(filePathAndFileName).length()];
            message.setFileName(fileName);
            fileInputStream = new FileInputStream(filePathAndFileName);
            fileInputStream.read(bytes);
            message.setBytes(bytes);

            oos = new ObjectOutputStream(ManagerClientConnectServerThread.getClientConnectServerThread(senderId).getSocket().getOutputStream());
            oos.writeObject(message);

            System.out.println("\n[" + message.getSentTime() + "]   " +
                    senderId + "给" + getterId + "发送文件,文件名为:" + fileName);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
//            if (!Objects.isNull(oos)) {
//                try {
//                    oos.close();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }

            if (!Objects.isNull(fileInputStream)) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }


}
